If you are on a Windows machine, you can install bash by following this guide. If you’re on macOS Catalina, you’ll be using the zsh shell in Terminal. Both of these are similar, but for today’s workshop, we will be using the command line in the RStudio.Cloud interface.
The command line, or command line interface CLI, is a text-based screen where people interact with their computer’s programs, files, and operating system using a combination of commands and parameters.
Command lines are everywhere, so being able to use them is helpful. This basic design might make the CLI sound inferior to a track-pad or touchscreen, but after a few examples of what’s possible from on the command-line and you’ll see the power of using these tools.
It might seem clunky and ancient, but people keep this technology around because of it’s 1) specificity and 2) modularity.
Specificity means each Unix command or tool does one thing very well (or DOTADIW
Modularity is the ability to mix and match these tools together with ‘pipes,’ a kind of grammatical glue that allows users to expand these tools in seemingly endless combinations
“This is the Unix philosophy: Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface.” — Doug McIlory
VS
Is it cooler?
Having these skills have also made us more comfortable when we’ve had to interact with remote machines or different operating systems (Linux, per se). We will work through an example to demonstrate some of these features.
A shell is an interface for users to interactively provide instructions and data to the operating system.
tcshiterm2bashzshOn Macs, the Terminal application runs a bash shell, which is why you can see the bash -- 86x25 on the top of the window. bash is a commonly-used shell, but there are other options too (see Zsh, tcsh, and sh). Fun fact: bash is a pun for the sh shell: bourne-again shell.
The most common you’ll encounter are probably bash and zsh, maybe be tsch.
After logging into RStudio.Cloud, you should create a new project. You can click on the New Project button, and it will give you the option to open a New Project, or a New Project from Git Repo. We want the first.
Follow the image to get a new Terminal instance in RStudio.Cloud.
FAIR WARNING–command-line interfaces can be frustrating. Computers don’t behave in ways that are easy to understand (that’s why GUIs exist). Switching from a GUI to a CLI seems like a step backward at first, but the initial headaches pay off because of the gains we’ll have in control, flexibility, automation, and reproducibility.
Here is a quick list of commonly used Terminal commands.
pwd - print working directory
cd - change directories
cp - copy files from one directory to another
ls - list all files
ls -la - list all files, including hidden ones
mkdir - make directory
rmdir - remove a directory
cat - display a text file in Terminal screen
echo - outputs text as arguments, prints to Terminal screen, file, or in a pipeline
touch - create a few files
grep - “globally search a regular expression and print”
>> and > - redirect output of program to a file (don’t display on Terminal screen)
sudo and sudo -s (BE CAREFUL!!) performing commands as root user can carry some heavy consequences.
We can check by inspecting the $SHELL variable.
$ echo $SHELL
/bin/bash
We’re using the bash shell in RStudio.Cloud.
Check the login with whoami?
$ whoami
rstudio-user
We are signed is as rstudio-user. This is important if we are wondering about privileges.
$ pwd
cloud/project
This tells us we’re in the cloud/project folder. We can also see this in the files pane.
We will create a new folder, data, using mkdir. You won’t see anything happen when you enter this command.
$ mkdir data
Now we’ll create a README.md file to document everything in this new project. README’s are an important part of data work, you can read more about what goes in them here. You won’t see anything after entering these commands.
$ touch README.md
Next we will be editing the README file. We want to add a title (read more about the markdown (md) syntax here). We will be doing this with the echo command and the greater than sign (>). This tells bash to add whatever text comes after echo belongs in the file after the greater than sign. It looks like this:
$ echo "# My Frankenstien" > README.md
Again, we’ll see no evidence of what we’ve done in the terminal, so we will check the file to see if it worked.
OOPS! We have a typo. We can fix it by entering the same commands (with correct spelling).
$ echo "# My Frankenstien" > README.md
$ # fix typo
$ echo "# My Frankenstein" > README.md
$ echo $SHELL
OOPS! We have a typo. We can fix it by entering the same commands (with correct spelling).